package com.itcast.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import org.springframework.beans.factory.annotation.Value;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;


public class OSSUtils {
    private static String endpoint  = "oss-cn-shenzhen.aliyuncs.com";

    private static String accessKeyId = "xxxxxx";
    private static String accessKeySecret = "xxxxxx";

    private static String bucketName = "xxxxxx-bk";

    private static OSS ossClient;


    /**
     *@Title:uploadByInputStream
     *@Description 流上传
     *@Param ossPath OSS上的路径
     */
    public static void upload(String fileName, InputStream inputStream) {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, fileName,inputStream);
        }catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        }finally{
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     *@Title:uploadByInputStream
     *@Description 上传Byte数组
     *@Param fileName 文件名
     */
    public static void upload(String fileName,byte[] bytes) {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, fileName,new ByteArrayInputStream(bytes));
        }catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        }finally{
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     *@Title:download
     *@Description 流式下载
     *@param filePath 本地路径
     *@Param ossPath OSS上的路径
     */
    public static void download(String ossPath,String filePath) throws Exception{
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, ossPath), new File(filePath));
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());

        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * @Title: delete
     * @param ossPath OSS上的路径
     * @Description:
     */
    public static void delete(String ossPath){
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.deleteObject(bucketName, ossPath);
        } catch (OSSException oe) {
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
        } catch (ClientException ce) {
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}
